package dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import dao.MessagesDao;
import entity.Messages;

public class MessagesDaoImpl extends BaseDaoImpl<Messages> implements MessagesDao{

	@Override
	public List<Messages> getMessage(int pageNum,int pageSize) {
		String sql= "SELECT * FROM Messages ORDER BY PublishDate DESC,id DESC limit ?,?";
		int p1=(pageNum-1)*pageSize;
		int p2=pageSize;
		return showAll(sql,p1,p2);
	}

	@Override
	public List<Messages> listByRs(ResultSet rs) {
		List<Messages> list =new ArrayList<Messages>();
		try {
			while(rs.next()){
				list.add(new Messages(rs.getInt("id"),
						rs.getString("username"),
						rs.getString("phone"),
						rs.getString("email"),
						rs.getString("content"),
						rs.getDate("PublishDate"),
						rs.getInt("IsRead")));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public void addMessage(Messages m) {
		String sql="INSERT INTO Messages(UserName,Phone,Email,Content,PublishDate,IsRead) values (?,?,?,?,?,?)";
		CUDObject(sql, m.getUsername(),m.getPhone(),m.getEmail(),m.getContent(),m.getPublishdate(),m.getIsread());
	}

	@Override
	public int getAllMessage() {
		String sql= "SELECT * FROM Messages";
		return showAll(sql).size();
	}



}
